package pt.isec.a21210392.a21190220.ludotwist;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

import pt.isec.a21210392.a21190220.ludotwist.GameLogic.Game;
import pt.isec.a21210392.a21190220.ludotwist.GameLogic.Human;
import pt.isec.a21210392.a21190220.ludotwist.GameLogic.Player;
import android.util.Log;

public class PlayerThreadOnHost extends Thread
{
	private int playerNumber;
	private HostGameActivity.HostThread host;
	private Socket socket;
	private boolean play = false;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private Message message;
	
	public PlayerThreadOnHost(HostGameActivity.HostThread host, Socket socket) 
	{
		this.host = host;
		this.socket = socket;
		try
		{
			this.out = new ObjectOutputStream(socket.getOutputStream());
		}
		catch(Exception e){Log.d("No construtor:",e.getMessage().toString());}
	}
	
	public void joinGame(int playerNumber)
	{
		this.playerNumber = playerNumber;
		play = true;
	}
	public void sendName(Human human)
	{
		try
		{
			out.writeObject(new Message(Constants.NAME, human));
			out.flush();
		}
		catch(Exception e){Log.d("sendName:", e.getMessage().toString());}
	}
	public void play(Game game)
	{
		try
		{
			out.reset();
			out.writeObject(new Message(Constants.PLAY, game));
			out.flush();
		}
		catch(Exception e){Log.d("play:", e.getMessage().toString());}
	}
	public void updateBoard(ArrayList<ToDraw> draws)
	{
		try
		{
			out.reset();
			out.writeObject(new Message(Constants.UPDATE_BOARD,draws));
			out.flush();
		}
		catch(Exception e){Log.d("update:", e.getMessage().toString());}
	}
	public void updateDice(int result)
	{
		try
		{
			out.reset();
			out.writeObject(new Message(Constants.UPDATE_DICE,result));
			out.flush();
		}
		catch(Exception e){Log.d("update:", e.getMessage().toString());}
	}
	public void finishTurn(Game game)
	{
		try
		{
			out.reset();
			out.writeObject(new Message(Constants.FINISH_TURN,game));
			out.flush();
		}
		catch(Exception e){Log.d("finishTurn:", e.getMessage().toString());}
	}
	public void finishPlay(Player winner)
	{
		try
		{
			out.writeObject(new Message(Constants.FINISH_PLAY,winner));
			out.flush();
		}
		catch(Exception e){Log.d("finishPlay:", e.getMessage().toString());}
	}
	
	@Override
	public void run()
	{		
		while(!play){}
		try
		{
			out.writeObject(new Message(Constants.START,playerNumber));
			out.flush();
			Log.d("in","a abrir...");
			in = new ObjectInputStream(socket.getInputStream());
			Log.d("in","abriu!");
		}
		catch(Exception e){Log.d("run:", e.getMessage().toString());}
		
		while(play)
		{			
			try
			{
				message = (Message)in.readObject();
				Log.d("Em PlayerThread", message.getMessage());
				
				if (message.getMessage().equals(Constants.NAME))
				{
					host.sendName((Human)message.getObject());
					continue;
				}
				if (message.getMessage().equals(Constants.PLAY))
				{
					host.play((Game)message.getObject());
					continue;
				}
				if (message.getMessage().equals(Constants.UPDATE_BOARD))
				{
					host.updateBoard((ArrayList<ToDraw>)message.getObject(), this);
					continue;
				}
				if (message.getMessage().equals(Constants.UPDATE_DICE))
				{
					host.updateDice((Integer)message.getObject(), this);
					continue;
				}
				if (message.getMessage().equals(Constants.FINISH_TURN))
				{
					host.finishTurn((Game)message.getObject());
					continue;
				}
				if (message.getMessage().equals(Constants.FINISH_PLAY))
				{
					host.finishPlay((Player)message.getObject());
				}
				if (message.getMessage().equals(Constants.SET_FINISH))
				{
					play = false;
				}
			}
			catch(Exception e){Log.d("Em PlayerThread",e.getMessage().toString());e.printStackTrace();}
		}
	}
}
